﻿//  Copyright (c) 2011 Ray Liang (http://www.dotnetage.com)
//  Dual licensed under the MIT and GPL licenses:
//  http://www.opensource.org/licenses/mit-license.php
//  http://www.gnu.org/licenses/gpl.html

using System;
using System.Web.Mvc;
using DNA.Mvc.jQuery;
using DNA.Mvc.Management;
using System.Collections.Generic;
using System.Linq;
using System.Web.Helpers;

namespace DNA.Mvc.Controllers
{
    public class HealthController : Controller
    {
        private ILogRepository _logRepository;
        private IWebStatRepository _statRepository;

        private string GetWebSiteName(IDictionary<string, object> values)
        {
            return values.ContainsKey("website") ? values["website"].ToString() : "home";
        }

        public HealthController(ILogRepository logRepository, IWebStatRepository statRepository)
        {
            _logRepository = logRepository;
            _statRepository = statRepository;
        }

        [SiteControlPanel(ResKey = "RES_LOGVIEWER", Sequence = 10, ShowInTopSiteOnly = true), Pagable]
        [SecurityAction("Management base", "View log", "Allows users can view or clear the system log.",
            PermssionSetResName = "SA_Managementbase",
            TitleResName = "SA_ViewLog",
            DescResName = "SA_ViewLogDesc"
            )]
        public ActionResult Index(QueryParams query)
        {
            var total = 0;
            var logs = _logRepository.All(out total, query.Index - 1, query.Size).ToList();
            return View(new ModelWrapper()
            {
                Total = total,
                Model = logs
            });
        }

        [HttpPost]
        public ActionResult Index()
        {
            _logRepository.Clear();
            return RedirectToAction("Index");
        }

        public ActionResult Referring(int maxResults=10)
        { 
           return PartialView(_statRepository.GetIncomingLinks(GetWebSiteName(RouteData.Values),maxResults));
        }

        [Authorize]
        public ActionResult TrafficChart(string part = "d", int? w = 300, int? h = 300)
        {
            var visitors = _statRepository.GetVisitors(GetWebSiteName(RouteData.Values), DateTime.UtcNow.AddDays(-10), DateTime.UtcNow);
            return File(GetChart("Traffic", visitors, chartType: "Area", w: w.Value, h: h.Value), "image/png");
        }

        [Authorize]
        public ActionResult BrowserChart(int? w = 300, int? h = 300)
        {
            var browsers = _statRepository.GetBrowsers(GetWebSiteName(RouteData.Values), DateTime.UtcNow.AddDays(-10), DateTime.UtcNow);
            return File(GetChart("Browsers", browsers.Cast<StatEntry>(), w: w.Value, h: h.Value), "image/png");
        }

        [Authorize]
        public ActionResult PageViewChart(int? w = 300, int? h = 300)
        {
            var visitors = _statRepository.GetPageViews(GetWebSiteName(RouteData.Values), DateTime.UtcNow.AddDays(-10), DateTime.UtcNow);
            return File(GetChart("Traffic", visitors, chartType: "Area", w: w.Value, h: h.Value), "image/png");
            //var crawlers = _statRepository.GetCrawlers(GetWebSiteName(RouteData.Values), DateTime.UtcNow.AddDays(-10), DateTime.UtcNow);
            //return File(GetChart("Crawlers", crawlers, chartType: "Bar", w: w.Value, h: h.Value), "image/png");
        }

        private static byte[] GetChart(string title, IEnumerable<StatEntry> entries, string format = "png", string chartType = "Column", int w = 300, int h = 300)
        {
            var chart = new Chart(w, h)
                .AddSeries(name: title, chartType: chartType,
            xValue: entries.Select(s => (s.Title)).ToArray(),
            yValues: entries.Select(s => s.Count.ToString()).ToArray());
            return chart.ToWebImage(format: format).GetBytes();
        }


    }
}
